package com.easychat.common;

import com.easychat.enums.ResultCodeEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @author 10 2024/10/2
 */
@Data
public class Result <T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer code;
    private String message;
    private T data;
    Result(){

    }
    protected static <T> Result<T> build(T data){
        Result<T> tResult = new Result<>();
        if(data != null){
            tResult.setData(data);
        }
        return tResult;
    }
    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum){
        Result<T> tResult = build(body);
        tResult.setCode(resultCodeEnum.getCode());
        tResult.setMessage(resultCodeEnum.getMessage());
        return tResult;
    }
    public static <T> Result<T> ok(){
        return Result.ok(null);
    }
    public static <T> Result<T> ok(T data){
        return build(data,ResultCodeEnum.SUCCESS);
    }
    public static <T> Result <T> fail(){
        return Result.fail(null);
    }
    public static <T> Result <T> fail(T data){
        return build(data,ResultCodeEnum.FAIL);
    }
    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }

}
